﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using RestSharp;

namespace Gbu.BLTool.BaseTool
{
    public class EntityTool
    {
        public static List<Parameter> GetProperties<T>(T t)
        {
            var plist = new List<Parameter>();
            if (t == null)
            {
                return plist;
            }

            var properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return plist;
            }

            foreach (var pi in properties)
            {
                var name = pi.Name;//属性名
                var value = pi.GetValue(t, null);//属性值
                var desc =((DescriptionAttribute) Attribute.GetCustomAttribute(pi, typeof (DescriptionAttribute))).Description; //描述属性标记值([Description("hahah")])
                
                ParameterType ptype=ParameterType.RequestBody;

                if (desc == "UrlSeg")
                {
                    ptype=ParameterType.UrlSegment;
                }

                plist.Add(new Parameter(){Name = name,Value = value,Type =ptype });

            }
            return plist;
        }
    }
}
